package com.haiheng.library_base.dialog

import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.haiheng.library_base.R
import com.haiheng.library_base.databinding.DialogPasswordBinding
import com.haiheng.library_base.utils.KeyboardUtils
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeCompat


/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/7/14 0014
 * @Version: 1.0
 */
class PasswordDialog : BaseDialog() {
    private lateinit var binding: DialogPasswordBinding
    private var onPasswordLisener: OnPasswordLisener? = null
    private var title = ""
    companion object{
        @JvmStatic
        fun newInstance(): PasswordDialog{
            return PasswordDialog()
        }
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        AutoSizeCompat.autoConvertDensityOfGlobal(getResources());//如果没有自定义需求用这个方法
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        binding = DialogPasswordBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCanceledOnTouchOutside(false)
        dialog?.window?.apply { setDimAmount(0.5f) }
        binding?.tvDialogLeft.setOnClickListener {
            dismiss()
        }
        binding?.tvDialogRight.setOnClickListener {
            if (onPasswordLisener != null){
                onPasswordLisener?.onRightClick(binding?.tvTitle?.text.toString(),binding?.pwdEditText?.pwdText)
            }
        }
        iniDate()
    }

    private fun iniDate() {
        updataTitleText()
    }


    fun updataTitleText(){
        if (this::binding.isInitialized && !TextUtils.isEmpty(title)){
            binding?.tvTitle?.setText(title)
        }
    }

    fun setDialogTitle(title: String):PasswordDialog {
        this.title = title
        updataTitleText()
        return this
    }

    fun clearText() {
        binding?.pwdEditText?.clearText()
    }

    fun getOnPasswordLisener(): OnPasswordLisener? {
        return onPasswordLisener
    }

    fun setOnPasswordLisener(onPasswordLisener: OnPasswordLisener?) {
        this.onPasswordLisener = onPasswordLisener
    }

    override fun dismiss() {
        KeyboardUtils.hideKeyboard(binding.root)
        super.dismiss()
    }

    interface OnPasswordLisener {
        fun onRightClick(title: String?, passwod: String?)
    }
}